/*
Copyright (c) [2019年5月1日] [吴超]
[MBT_Studio] is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
		 http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
*/
#include "stdafx.h"
#include "加载保存属性.h"

#include <文件/file_载入保存数据.h>
#include "core/动画/an_曲线.h"
#include "core/属性.h"




void f_prop_Load(S_Props& prop, FILE* f) {
	uint32 type = 0;
	//fread(&type, sizeof(uint32), 1, f);
	switch (prop.m_Type)
	{
	case E_值类型::e_Type_Bool:
		fread(prop.m_Value, sizeof(bool), 1, f);
		break;
	case E_值类型::e_Type_F32:
		fread(prop.m_Value, sizeof(float32), 1, f);
		break;
	case E_值类型::e_Type_I32:
		fread(prop.m_Value, sizeof(int32), 1, f);
		break;
	case E_值类型::e_Type_UI32:
		fread(prop.m_Value, sizeof(uint32), 1, f);
		break;
	case E_值类型::e_Type_Enum:
		fread(&f_prop_enum(prop), sizeof(int32), 1, f);
		break;
	case E_值类型::e_Type_Vec2:
		fread(prop.m_Value, sizeof(vec2), 1, f);
		break;
	case E_值类型::e_Type_Vec3:
		fread(prop.m_Value, sizeof(vec3), 1, f);
		break;
	case E_值类型::e_Type_iVec2:
		fread(prop.m_Value, sizeof(ivec2), 1, f);
		break;
	case E_值类型::e_Type_uVec2:
		fread(prop.m_Value, sizeof(uvec2), 1, f);
		break;
	case E_值类型::e_Type_路径:
		f_prop_Path(prop) = f_读取字符串(f);
		break;
	case E_值类型::e_Type_Str:
		f_prop_Str(prop) = f_file_读取字符串(f);
		break;
	case E_值类型::e_Type_LineText:
		f_prop_LineStr(prop) = f_file_读取字符串(f);
		break;
	case E_值类型::e_Type_2D曲线:
		 f_an_Load(f_prop_2DCurve(prop), f);
		break;
	case E_值类型::e_Type_Bool_1D: {
		auto item = f_prop_Bool_1D(prop);
		fread(&prop.m_值元素个数, sizeof(uint8), 1, f);
		for (uint32 i = 0; i < prop.m_值元素个数; ++i) {
			fread(&item->m_Value, sizeof(uint8), 1, f);
		}
		break;
	}
	case E_值类型::e_Type_MapPtr_Array1D: {
		S_MapPtr& e = f_prop_MapPrt(prop);
		fread(&e.m_当前项, sizeof(uint64), 1, f);
		break;
	}
	case E_值类型::e_Type_Object_1D:
		break;
	default:
		break;
	}

}

void f_prop_Save(S_Props& prop, FILE* f) {
	uint32 type = prop.m_Type;
	
	switch (prop.m_Type) {
	case E_值类型::e_Type_Bool:
		fwrite(prop.m_Value, sizeof(bool), 1, f);
		break;
	case E_值类型::e_Type_F32:
		fwrite(prop.m_Value, sizeof(float32), 1, f);
		break;
	case E_值类型::e_Type_I32:
		fwrite(prop.m_Value, sizeof(int32), 1, f);
		break;
	case E_值类型::e_Type_UI32:
		fwrite(prop.m_Value, sizeof(uint32), 1, f);
		break;
	case E_值类型::e_Type_Enum:
		fwrite(&f_prop_enum(prop), sizeof(int32), 1, f);
		break;
	case E_值类型::e_Type_Vec2:
		fwrite(prop.m_Value, sizeof(vec2), 1, f);
		break;
	case E_值类型::e_Type_Vec3:
		fwrite(prop.m_Value, sizeof(vec3), 1, f);
		break;
	case E_值类型::e_Type_iVec2:
		fwrite(prop.m_Value, sizeof(ivec2), 1, f);
		break;
	case E_值类型::e_Type_uVec2:
		fwrite(prop.m_Value, sizeof(uvec2), 1, f);
		break;
	case E_值类型::e_Type_路径:
		f_file_保存字符串(f, f_prop_Path(prop).string());
		break;
	case E_值类型::e_Type_Str:
		f_file_保存字符串(f, f_prop_Str(prop));
		break;
	case E_值类型::e_Type_LineText:
		f_file_保存字符串(f, f_prop_LineStr(prop));
		break;
	case E_值类型::e_Type_2D曲线:
		f_an_Save(f_prop_2DCurve(prop), f);
		break;
	case E_值类型::e_Type_Bool_1D: {
		auto item = f_prop_Bool_1D(prop);
		fwrite(&prop.m_值元素个数, sizeof(uint8), 1, f);
		for (uint32 i = 0; i < prop.m_值元素个数; ++i) {
			fwrite(&item->m_Value, sizeof(uint8), 1, f);
		}
		break;
	}
	case E_值类型::e_Type_MapPtr_Array1D: {
		S_MapPtr& e = f_prop_MapPrt(prop);
		fwrite(&e.m_当前项, sizeof(uint64), 1, f);
		break;
	}
	case E_值类型::e_Type_Object_1D:
		break;
	default:
		break;
	}
}







const uint8* f_prop_Load(S_Props& prop, const uint8* 块) {
	uint32 type = 0;
	//fread(&type, sizeof(uint32), 1, f);
	switch (prop.m_Type) {
		case E_值类型::e_Type_Bool:
			*((bool*)prop.m_Value) = *((bool*)块);
			return &块[1];
		case E_值类型::e_Type_I8:
			*((int8*)prop.m_Value) = *((int8*)块);
			return &块[1];
		case E_值类型::e_Type_F32:
			*((float32*)prop.m_Value) = *((float32*)块);
			return &块[sizeof(float32)];
		case E_值类型::e_Type_I32:
			*((int32*)prop.m_Value) = *((int32*)块);
			return &块[sizeof(int32)];

		case E_值类型::e_Type_UI32_FlagBits:
		case E_值类型::e_Type_UI32:
			*((uint32*)prop.m_Value) = *((uint32*)块);
			return &块[sizeof(uint32)];

		case E_值类型::e_Type_Enum:
			f_prop_enum(prop) = *((int32*)块);
			return &块[sizeof(uint32)];
		case E_值类型::e_Type_Vec2:
			*((vec2*)prop.m_Value) = *((vec2*)块);
			return &块[sizeof(vec2)];
		case E_值类型::e_Type_Vec3:
			*((vec3*)prop.m_Value) = *((vec3*)块);
			return &块[sizeof(vec3)];
		case E_值类型::e_Type_iVec2:
			*((ivec2*)prop.m_Value) = *((ivec2*)块);
			return &块[sizeof(ivec2)];
		case E_值类型::e_Type_iVec3:
			*((ivec3*)prop.m_Value) = *((ivec3*)块);
			return &块[sizeof(ivec3)];
		case E_值类型::e_Type_uVec2:
			*((uvec2*)prop.m_Value) = *((uvec2*)块);
			return &块[sizeof(uvec2)];
		case E_值类型::e_Type_路径: {
			std::string str;
			const uint8* p = f_解字符串(str, 块);
			f_prop_Path(prop) = str;
			return p;
		}
		case E_值类型::e_Type_Str:
			 return f_解宽字符串(f_prop_Str(prop), 块);
			break;
		case E_值类型::e_Type_LineText:
			return f_解宽字符串(f_prop_LineStr(prop), 块);
			
		case E_值类型::e_Type_2D曲线:
			return f_an_解(f_prop_2DCurve(prop), 块);
			
		case E_值类型::e_Type_Bool_1D: {
			auto item = f_prop_Bool_1D(prop);

			prop.m_值元素个数 = *((uint8*)块);
			块 = &块[1];
			memcpy(&item->m_Value, &块, prop.m_值元素个数);
			
			return &块[prop.m_值元素个数];
		}
		case E_值类型::e_Type_MapPtr_Array1D: {
			S_MapPtr& e = f_prop_MapPrt(prop);
			e.m_当前项 = *((uint64*)块);
			return &块[sizeof(uint64)];
		}
		case E_值类型::e_Type_Object_1D:
			break;
		default:
			return &块[1];
	}
	return nullptr;
}

void f_prop_Save(S_Props& prop, S_UI8Array* 块) {
	uint32 type = prop.m_Type;

	switch (prop.m_Type) {
		case E_值类型::e_Type_Bool:
			f_core_array_push_back_block((S_Array*)块, (uint8*)prop.m_Value, sizeof(bool));
			break;
		case E_值类型::e_Type_I8:
			f_core_array_push_back_block((S_Array*)块, (uint8*)prop.m_Value, sizeof(int8));
			break;
		case E_值类型::e_Type_F32:
			f_core_array_push_back_block((S_Array*)块, (uint8*)prop.m_Value, sizeof(float32));
			break;
		case E_值类型::e_Type_I32:
			f_core_array_push_back_block((S_Array*)块, (uint8*)prop.m_Value, sizeof(int32));
			break;

		case E_值类型::e_Type_UI32_FlagBits:
		case E_值类型::e_Type_UI32:
			f_core_array_push_back_block((S_Array*)块, (uint8*)prop.m_Value, sizeof(uint32));
			break;
		case E_值类型::e_Type_Enum:
			f_core_array_push_back_block((S_Array*)块, (uint8*)&f_prop_enum(prop), sizeof(int32));
			break;
		case E_值类型::e_Type_Vec2:
			f_core_array_push_back_block((S_Array*)块, (uint8*)prop.m_Value, sizeof(vec2));
			break;
		case E_值类型::e_Type_Vec3:
			f_core_array_push_back_block((S_Array*)块, (uint8*)prop.m_Value, sizeof(vec3));
			break;
		case E_值类型::e_Type_iVec2:
			f_core_array_push_back_block((S_Array*)块, (uint8*)prop.m_Value, sizeof(ivec2));
			break;
		case E_值类型::e_Type_iVec3:
			f_core_array_push_back_block((S_Array*)块, (uint8*)prop.m_Value, sizeof(ivec3));
			break;
		case E_值类型::e_Type_uVec2:
			f_core_array_push_back_block((S_Array*)块, (uint8*)prop.m_Value, sizeof(uvec2));
			break;
		case E_值类型::e_Type_路径:
			f_包字符串(块, f_prop_Path(prop).string());
			break;
		case E_值类型::e_Type_Str:
			f_包宽字符串(块, f_prop_Str(prop));
			break;
		case E_值类型::e_Type_LineText:
			f_包宽字符串(块, f_prop_LineStr(prop));
			break;
		case E_值类型::e_Type_2D曲线:
			f_an_包(f_prop_2DCurve(prop), 块);
			break;
		case E_值类型::e_Type_Bool_1D: {
			auto item = f_prop_Bool_1D(prop);

			f_core_array_push_back_block((S_Array*)块, (uint8*)&prop.m_值元素个数, sizeof(uint8));
			f_core_array_push_back_block((S_Array*)块, (uint8*)&item->m_Value, prop.m_值元素个数 * sizeof(uint8));
			break;
		}
		case E_值类型::e_Type_MapPtr_Array1D: {
			S_MapPtr& e = f_prop_MapPrt(prop);
			f_core_array_push_back_block((S_Array*)块, (uint8*)&e.m_当前项, sizeof(uint64));
			break;
		}
		case E_值类型::e_Type_Object_1D:
			break;
		default:
			f_core_array_push_back_block((S_Array*)块, (uint8*)prop.m_Value, 1);
			break;
	}
}



